package com.example.agriapp.view.activity;

import android.content.Intent;
import android.graphics.Color;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;

import com.example.agriapp.base.BaseActivity;
import com.example.agriapp.R;
import com.example.agriapp.util.SharedPrefUtils;
import com.example.agriapp.view.fragment.AgriTechFragment;
import com.example.agriapp.view.fragment.FriendsCircleFragment;
import com.example.agriapp.view.fragment.HomeFragment;
import com.example.agriapp.view.fragment.MessageFragment;
import com.example.agriapp.view.fragment.ProfileFragment;
import com.example.agriapp.view.fragment.SellerHomeFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;

public class MainActivity extends BaseActivity {
    private BottomNavigationView bottomNavigationView;
    private Fragment currentFragment;
    private RelativeLayout relativeLayout;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        getWindow().setStatusBarColor(ContextCompat.getColor(this, R.color.bottom_nav_icon_selected));
        bottomNavigationView = findViewById(R.id.bottom_nav);
        relativeLayout = findViewById(R.id.rePublic);
        String username = SharedPrefUtils.getString(MainActivity.this, "USERNAME", "");
        findViewById(R.id.faHuoPin).setOnClickListener(v -> {
            startActivity(new Intent(MainActivity.this, PublishActivity.class));
        });

        findViewById(R.id.faCaiGou).setOnClickListener(view -> {
            startActivity(new Intent(MainActivity.this, PublishActivity.class));
        });

        TextView tvPublic = findViewById(R.id.tvPublic);
        LinearLayout lin = findViewById(R.id.lin);
        ImageView closeButton = findViewById(R.id.closeButton);
        closeButton.setOnClickListener(view -> {
            relativeLayout.setVisibility(View.GONE);
        });

        lin.setOnClickListener(v -> {
            Toast.makeText(this, "点击了", Toast.LENGTH_SHORT).show();
            tvPublic.setTextColor(Color.BLACK);
            tvPublic.setTextSize(14);
            relativeLayout.setVisibility(View.VISIBLE);
        });
        Menu menu = bottomNavigationView.getMenu();

        if (username.equals("admin")) {
            menu.findItem(R.id.nav_community).setTitle("农友圈");
            menu.findItem(R.id.nav_community).setIcon(R.drawable.ic_people_outline);
        } else {
            menu.findItem(R.id.nav_community).setTitle("农技学堂");
            menu.findItem(R.id.nav_community).setIcon(R.drawable.ic_school);
        }

        bottomNavigationView.setOnNavigationItemSelectedListener(item -> {
            if (lin.getVisibility() == View.VISIBLE) {
                tvPublic.setTextColor(R.color.colorNavUnselected);
                tvPublic.setTextSize(13);
                relativeLayout.setVisibility(View.GONE);
            }

            int itemId = item.getItemId();
            if (itemId == R.id.nav_home) {
                if (username.equals("admin")) {
                    switchFragment(SellerHomeFragment.newInstance());
                } else {
                    switchFragment(HomeFragment.newInstance());
                }
            } else if (itemId == R.id.nav_community) {
                if (username.equals("admin")) {
                    switchFragment(FriendsCircleFragment.newInstance());
                } else {
                    switchFragment(AgriTechFragment.newInstance());
                }
            } else if (itemId == R.id.nav_message) {
                switchFragment(MessageFragment.newInstance());
            } else if (itemId == R.id.nav_profile) {
                switchFragment(ProfileFragment.newInstance());
            }
            return true;
        });

        // 默认显示首页
        if (username.equals("admin")) {
            switchFragment(SellerHomeFragment.newInstance());
        } else {
            switchFragment(HomeFragment.newInstance());
        }
    }

    private void switchFragment(Fragment fragment) {
        if (currentFragment == null || !currentFragment.getClass().equals(fragment.getClass())) {
            getSupportFragmentManager().beginTransaction()
                    .replace(R.id.container, fragment)
                    .commit();
            currentFragment = fragment;
        }
    }

    @Override
    protected void initData() {
        // 初始化数据
    }
}